package com.polaris.live.ui.message.widget.single_message

import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.polaris.live.adapter.MessageAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.autoDp
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.MessageItemInviteBinding
import com.polaris.live.im.db.entity.MessageEntity
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.avatar
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.widget.avatar.FrameImageUtils
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

/**
 * 邀请进房消息
 *
 * @author Created by lucas on 2024/11/25 20:19
 * @since 1.0.0
 */
class MessageInviteItemHolder(
    adapter: MessageAdapter,
    binding: MessageItemInviteBinding,
) : AbstractMessageItemHolder<MessageItemInviteBinding>(adapter, binding) {

    override fun bind(item: MessageEntity) {
        mBinding.apply {
            bindCommon(
                item,
                layoutTime.messageTextTime,
                layoutLeft,
                layoutRight,
                layoutLeftAvatar.avAvatar,
                layoutRightAvatar.avAvatar,
                layoutLeftAvatar.avFv,
                layoutRightAvatar.avFv,
                null,
                null,
                ivState,
            )

            if (item.sender == UserManager.getUser().userId) {
                setRight(item)
            } else {
                setLeft(item)
            }
        }
    }

    private fun setLeft(item: MessageEntity) {
        item.payloadBean?.let {
            when (it.liveType) {
                LiveConst.Type.PARTY_LIVE, LiveConst.Type.PARTY_GAME_LIVE, LiveConst.Type.VIDEO_PARTY -> { //派对
                    mBinding.bubbleLeftParty.visibility = View.VISIBLE
                    mBinding.bubbleLeftLive.visibility = View.GONE
                    Glide.with(mBinding.coverImgParty)
                        .load(ImageUtils.imgPath(it.anchor?.avatar))
                        .circleCrop()
                        .avatar()
                        .into(mBinding.coverImgParty)
                    mBinding.avFvLeft.setFrameImage(FrameImageUtils.urlToFrameImageBean(it.anchor?.avatarFrame))
                    val name = it.anchor?.nickname ?: ""
                    mBinding.nameTvParty.text = name
                    if (it.highlight != null) {
                        RouterUtils.handleRouterText(
                            mBinding.messageTvParty,
                            item.content,
                            it.highlight,
                            it.router ?: emptyMap()
                        )
                    } else {
                        mBinding.messageTvParty.text = item.content
                    }
                }

                LiveConst.Type.LIVE_AND_MATCH, LiveConst.Type.ONLY_MATCH, LiveConst.Type.ONLY_LIVE -> {//直播
                    mBinding.bubbleLeftParty.visibility = View.GONE
                    mBinding.bubbleLeftLive.visibility = View.VISIBLE
                    Glide.with(mBinding.coverImgLive)
                        .load(ImageUtils.imgPath(it.cover))
                        .transform(
                            CenterCrop(),
                            RoundedCornersTransformation(
                                8.autoDp,
                                0,
                                RoundedCornersTransformation.CornerType.TOP
                            )
                        )
                        .into(mBinding.coverImgLive)
                    if (it.highlight != null) {
                        RouterUtils.handleRouterText(
                            mBinding.messageTvLive,
                            item.content,
                            it.highlight,
                            it.router ?: emptyMap()
                        )
                    } else {
                        mBinding.messageTvLive.text = item.content
                    }
                }

                else -> {

                }
            }


        }
    }

    private fun setRight(item: MessageEntity) {
        item.payloadBean?.let {
            when (it.liveType) {
                LiveConst.Type.PARTY_LIVE, LiveConst.Type.PARTY_GAME_LIVE, LiveConst.Type.VIDEO_PARTY -> { //派对
                    mBinding.bubbleRightParty.visibility = View.VISIBLE
                    mBinding.bubbleRight.visibility = View.GONE
                    Glide.with(mBinding.coverImgRightParty)
                        .load(ImageUtils.imgPath(it.anchor?.avatar))
                        .circleCrop()
                        .avatar()
                        .into(mBinding.coverImgRightParty)
                    mBinding.avFvRight.setFrameImage(FrameImageUtils.urlToFrameImageBean(it.anchor?.avatarFrame))
                    val name = it.anchor?.nickname ?: ""
                    mBinding.nameTvRightParty.text = name
                    if (it.highlight != null) {
                        RouterUtils.handleRouterText(
                            mBinding.messageTvRightParty,
                            item.content,
                            it.highlight,
                            it.router ?: emptyMap()
                        )
                    } else {
                        mBinding.messageTvRightParty.text = item.content
                    }
                }

                LiveConst.Type.LIVE_AND_MATCH, LiveConst.Type.ONLY_MATCH, LiveConst.Type.ONLY_LIVE -> {//直播
                    mBinding.bubbleRightParty.visibility = View.GONE
                    mBinding.bubbleRight.visibility = View.VISIBLE
                    Glide.with(mBinding.coverImgRight)
                        .load(ImageUtils.imgPath(it.cover))
                        .transform(
                            CenterCrop(),
                            RoundedCornersTransformation(
                                8.autoDp,
                                0,
                                RoundedCornersTransformation.CornerType.TOP
                            )
                        )
                        .into(mBinding.coverImgRight)
                    if (it.highlight != null) {
                        RouterUtils.handleRouterText(
                            mBinding.messageTvRight,
                            item.content,
                            it.highlight,
                            it.router ?: emptyMap()
                        )
                    } else {
                        mBinding.messageTvRight.text = item.content
                    }
                }

            }


        }
    }
}